package com.hangwei.paperless.view.repair.accept;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.callback.ItemDragAndSwipeCallback;
import com.hangwei.paperless.MainRepairActivity;
import com.hangwei.paperless.R;
import com.hangwei.paperless.adapter.accept.Item3Adapter;
import com.hangwei.paperless.model.bean.AcceptCheckInfo;
import com.hangwei.paperless.presenter.StaticPresenter;
import com.hangwei.paperless.utils.JsonPostUtil;
import com.hangwei.paperless.view.BaseView;
import com.hangwei.paperless.widget.MaxRecyclerView;
import com.hangwei.paperless.widget.MyOnItemSwipeListener;
import com.twelve.all.net.CallBack;
import com.twelve.all.utils.LoadingDialog;
import com.twelve.all.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.RequestBody;

public class AcceptItem3 extends BaseView<MainRepairActivity, List<AcceptCheckInfo.TBean>> {

    private MaxRecyclerView mRecyclerView;
    private Item3Adapter mAdapter;

    public AcceptItem3(MainRepairActivity activity) {
        super(activity);
    }

//    @Override
//    public void release() {
//        EventBus.getDefault().unregister(mAdapter);
//        super.release();
//    }

    @Override
    protected View initView() {
        View inflate3 = LayoutInflater.from(mActivity).inflate(R.layout.page_accept_check_view3, null);
        TextView tvMsg3 = inflate3.findViewById(R.id.tvMsg);
        tvMsg3.setText("4、多余件 Surplus Parts");

        TextView tvSave0 = inflate3.findViewById(R.id.tvSave);
        TextView tvAdd = inflate3.findViewById(R.id.tvAdd);
        mRecyclerView = inflate3.findViewById(R.id.recyclerView);

        tvSave0.setOnClickListener(this);
        tvAdd.setOnClickListener(this);
        return inflate3;
    }

    @Override
    public void bindData(List<AcceptCheckInfo.TBean> list) {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
        mAdapter = new Item3Adapter(list) {
            @Override
            protected void scan(String id) {
                mActivity.objectId = id;
                mActivity.showCameraPop();
//                int scrollY = mScrollView.getScrollY();
//                Log.e(TAG, "scan:                           scrollY:"+scrollY);
            }

            @Override
            protected void showBigPic(ArrayList<String> picList, int position) {
                mActivity.showBigPic(picList,position);
            }
        };
//        EventBus.getDefault().register(mAdapter);
        mRecyclerView.setAdapter(mAdapter);

        ItemDragAndSwipeCallback itemDragAndSwipeCallback = new ItemDragAndSwipeCallback(mAdapter);
        itemDragAndSwipeCallback.setSwipeMoveFlags(ItemTouchHelper.START);
        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(itemDragAndSwipeCallback);
        //设置RecyclerView
        itemTouchHelper.attachToRecyclerView(mRecyclerView);
        // 开启滑动删除
        mAdapter.enableSwipeItem();
        mAdapter.setOnItemSwipeListener(new MyOnItemSwipeListener());
//        mRecyclerView.addOnItemTouchListener(new OnItemChildClickListener() {
//            @Override
//            public void onSimpleItemChildClick(BaseQuickAdapter adapter, View view, int position) {
////                AcceptCheckInfo.TBean bean = mAdapter.getData().get(position);
////                TextView tvQty = (TextView) adapter.getViewByPosition(mRecyclerView, position, R.id.tvQty);
//                switch (view.getId()) {
//                    case R.id.ivScan:
//                        mActivity.showCameraPop();
//                        break;
//                }
//            }
//        });
    }

    @Override
    protected void isNotFastClick(View v) {
        switch (v.getId()) {
            case R.id.tvAdd:
                int seq = 0;
                List<AcceptCheckInfo.TBean> data = mAdapter.getData();
                for (AcceptCheckInfo.TBean bean : data) {
                    if (bean.seq > seq)
                        seq = bean.seq;
                }
                AcceptCheckInfo.TBean tBean = new AcceptCheckInfo.TBean(true, seq + 1);
                data.add(tBean);
                mAdapter.setNewData(data);
                mRecyclerView.scrollToPosition(data.size() - 1);
                break;
            case R.id.tvSave:
                final LoadingDialog loadingDialog = new LoadingDialog(mActivity);
                loadingDialog.show();
                String jsonData = JsonPostUtil.buildAcceptJson(mAdapter.getData(), mActivity.getBillId(),4);
                RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), jsonData);
                StaticPresenter.saveReceiveRecordSheet(requestBody, new CallBack<List<AcceptCheckInfo.TBean>>() {
                    @Override
                    public void onCompleted(List<AcceptCheckInfo.TBean> list) {
                        loadingDialog.dismiss();
                        mAdapter.setNewData(list);
                        ToastUtil.showCenterToast("保存成功！");
                    }

                    @Override
                    public void onError() {
                        loadingDialog.dismiss();
                    }
                });
                break;
        }
    }
}
